package chpt02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class SortID {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<String> strList = new ArrayList<String>();
		for (int i = 0; i < n; i++) {
			strList.add(sc.next());
		}
		Collections.sort(strList, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				int x = o1.substring(6, 14).compareTo(o2.substring(6, 14));
				if (x > 0)
					return 1;
				else if (x < 0)
					return -1;
				return 0;
			}

		});

		while (true) {
			String choice = sc.next();
			if (choice.equals("sort1")) {
				for (String e : strList) {
					System.out.println(e.substring(6, 10) + "-" + e.substring(10, 12) + "-" + e.substring(12, 14));
				}
			} else if (choice.equals("sort2")) {
				for (String e : strList) {
					System.out.println(e);
				}

			} else {
				System.out.println("exit");
				break;
			}
		}
		sc.close();
	}

}
